package com.own.business.resource.core.base;

import com.own.business.resource.core.model.SignatureModel;
import com.own.business.resource.core.model.base.BaseUploadModel;
import com.own.business.resource.core.property.ResourceSourceProperty;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.exception.ExceptionEnum;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * AbstractObjectStoreUtil
 *
 * @author chenxueli
 * @date 2023-07-07 14:27:56
 */
@Slf4j
@Getter
public abstract class AbstractObjectStoreUtil implements BaseObjectStoreUtil<BaseUploadModel> {

    private String name;

    private ResourceSourceProperty properties;

    /**
     * 获取上传文件签名
     *
     * @param uri 资源路径
     * @return 签名对象
     */
    public SignatureModel signature(String uri) {
        throw new BusinessSimpleException(ExceptionEnum.Resource.SIGNATURE_UNDEFINED);
    }

    /**
     * 获取临时访问路径
     *
     * @param path 路径
     * @return 临时访问路径
     */
    public final String temporaryAccessPath(String path) {
        return temporaryAccessPath(path, null);
    }

    /**
     * 获取临时访问路径
     *
     * @param path  路径
     * @param style 样式
     * @return 临时访问路径
     */
    public final String temporaryAccessPath(String path, String style) {
        return temporaryAccessPath(path, 3600, style);
    }

    /**
     * 获取临时访问路径
     *
     * @param path       路径
     * @param style      样式
     * @param expiration 有效时间（秒）
     * @return 临时访问路径
     */
    public String temporaryAccessPath(String path, long expiration, String style) {
        return path;
    }

    /**
     * 设置配置信息并验证信息
     *
     * @param name       设置名称
     * @param properties 配置信息
     * @return 是否正常
     */
    public boolean setConfigAndCheck(String name, ResourceSourceProperty properties) {
        this.name = name;
        this.properties = properties;
        return this.check();
    }

    /**
     * 最后的上传位置
     *
     * @return 上传位置
     */
    @Override
    public String position() {
        return properties == null ? "localhost" : (properties.getPosition() + uploadPath());
    }

}
